<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\HttpException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * @param Exception $exception
     * @return mixed|void
     * @throws Exception
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param Request $request
     * @param Exception $exception
     * @return Response
     */
    public function render($request, Exception $exception)
    {
        if ($exception instanceof ApiKeyAuthException) {
            return response()->json([
                'code' => $exception->getCode(),
                'message' => $exception->getMessage(),
            ], $exception->getCode());
        }

        if ($exception instanceof AuthenticationException) {
            return response()->json([
                'code' => config('error.#401.status_code'),
                'message' => config('error.#401.message'),
            ], config('error.#401.status_code'));
        }

        if ($exception instanceof ModelNotFoundException) {
            return response()->json([
                'code' => config('error.#404.status_code'),
                'message' => config('error.#404.message'),
            ], config('error.#404.status_code'));
        }

        if ($exception instanceof ValidationException) {
            return response()->json([
                'code' => config('error.#422.status_code'),
                'message' => array_first($exception->errors())[0],
            ], config('error.#422.status_code'));
        }

        if ($exception instanceof HttpException) {
            if ($exception->getCode() === 0) {
                return response()->json([
                    'code' => $exception->getStatusCode(),
                    'message' => $exception->getMessage()
                        ? $exception->getMessage()
                        : config('error.#'.$exception->getStatusCode().'.message'),
                ], $exception->getStatusCode(), $exception->getHeaders());
            }

            return response()->json([
                'code' => $exception->getCode(),
                'message' => $exception->getMessage(),
            ], $exception->getStatusCode(), $exception->getHeaders());
        }

        return response()->json([
            'code' => config('error.#500.status_code'),
            'message' => config('error.#500.message'),
        ], config('error.#500.status_code'));
    }
}
